import 'package:flutter_base_kit/global/sp_key.dart';
import 'package:flutter_base_kit/global/user_helper.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

import '../bean/bean_app_setting.dart';
import '../common/permission_request_page.dart';
import '../common/user_protocol_page.dart';
import '../net/dio_utils.dart';
import '../net/loading_statues.dart';
import '../splash_page.dart';
import '../utils/log_util.dart';
import '../utils/sp_utils.dart';
import '../welcome_page.dart';

///代码清单12-4 启动页面对应控制器
class IndexController extends GetxController {
  ///网络请求加载
  LoadingStatues loadingStatues = LoadingStatues.loading;
  String errMsg = "";
  bool _userFirst = false;
  AppSettingBean? settingBean;

  @override
  void onReady() {
    super.onReady();
    checkPermissonFunction();
  }

  void checkPermissonFunction() async {
    await Get.dialog(
      const PermissionRequestPage(
        permissionList: [
          Permission.location,
          Permission.storage,
        ],
        noPassMessageList: ["获取位置权限", "获取文件存储权限"],
      ),
    );
    //初始化数据
    initData();
  }

  void initData({bool update = false}) async {
    //获取当前的运行环境
    //当App运行在Release环境时，inProduction为true；
    //当App运行在Debug和Profile环境时，inProduction为false。
    const bool inProduction = bool.fromEnvironment("dart.vm.product");
    //为ture时输出日志
    const bool isLog = !inProduction;
    //初始化统计
    //初始化本地存储工具
    await SPUtil.init();
    //初始化日志工具
    LogUtil.init(tag: "flutter_log", isDebug: isLog);
    bool isSuccess = await requestNet();
    if (!isSuccess) {
      return;
    }
    //获取用户是否第一次登录
    _userFirst = SPUtil.getBool(spUserIsFirstKey);
    //获取用户隐私协议的状态
    bool _userProtocol = SPUtil.getBool(spUserProtocolKey);
    //记录
    UserHelper.getInstance.userProtocol = _userProtocol;
    //初始化用户的登录信息
    UserHelper.getInstance.init();
    //下一步
    openUserProtocol();
  }

  Future<bool> requestNet() async {
    ResponseInfo responseInfo = await requestSetting();
    if (!responseInfo.success) {
      errMsg = responseInfo.message;
      loadingStatues = LoadingStatues.faile;
      update();
      return true;
    }
    return true;
  }

  Future<ResponseInfo> requestSetting() async {
    //网络请求获取APP的配置信息
    ResponseInfo responseInfo =
        await DioUtils.instance.getRequest(url: HttpHelper.SETTING_URL);
    if (responseInfo.success) {
      //解析数据
      settingBean = AppSettingBean.fromMap(responseInfo.data);
    }
    return responseInfo;
  }

  void openUserProtocol() async {
    //已同意用户隐私协议 下一步
    if (UserHelper.getInstance.isUserProtocol) {
      LogUtil.e("用户已同意用户隐私协议 下一步");
      openNext();
    } else {
      LogUtil.e("未同意用户协议 弹框显示");
      //未同意用户协议 弹框显示
      await Get.put(() => const UserProtocolRequestPage());
      openNext();
    }
  }

  void openNext() {
    //获取配置信息
    if (_userFirst == false) {
      LogUtil.e("第一次 进入引导页面");

      ///第一次 隐藏logo 显示左右滑动的引导
      Get.off(() => SplashPage());
    } else {
      LogUtil.e("非首次 进入首页面");
      Get.off(() => WelcomePage());
    }
  }
}
